from rest_framework import serializers
from rest_framework.serializers import StringRelatedField

# class StudentSerializer(serializers.Serializer):
#     id =serializers.IntegerField(label='ID', read_only=True)
#     student = serializers.CharField(label='姓名', max_length=20)
#     age = serializers.IntegerField(label='年龄', required=False)
#     sex = serializers.IntegerField(label='性别')
#     c_time = serializers.DateTimeField(label='创建时间')
#     u_time = serializers.DateTimeField(label='修改时间')
#     is_delete = serializers.BooleanField(label='逻辑删除')
from apps.study.models import Student, Classes


class StudentSerializer(serializers.ModelSerializer):
    # classes = StringRelatedField(read_only=True)
    class_name = serializers.CharField(read_only=True, source='classes.name')
    class Meta:
        model = Student
        fields = '__all__'

        extra_kwargs = {
            'age':{
                'min_value':0,
                'max_value':200,
            }
        }

class SimpleStudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = ['id', 'name']

#
#         # def validate_name(self, value):
#         #     if 'python' not in value.lower():
#         #         raise serializers.ValidationError('name error')
#         #     return  value
#
#         # def validate(self, attrs):
#         #     if 'python' not in attrs['name'].lower():
#         #         raise serializers.ValidationError('name error')
#         #     return attrs

class ClassesSerializer(serializers.ModelSerializer):
    student_set = SimpleStudentSerializer(read_only=True, many=True)

    class Meta:
        model = Classes
        fields = '__all__'


    def validate_name(self, value):
        if 'python' not in value:
            raise serializers.ValidationError('班级名必须带python')
        return value